<?php

namespace Rebecca\RebeccaBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Rebecca\RebeccaBundle\Entity\Contratos;
use Rebecca\RebeccaBundle\Form\ContratosType;
use Rebecca\RebeccaBundle\Entity\ParamertrosRegistrados;

/**
 * Contratos controller.
 *
 */
class ContratosController extends Controller {

    /**
     * Lists all Contratos entities.
     *
     */
    public function indexAction() {
        /* @var $component Rebecca\RebeccaBundle\Component\ContratosComponent */
        $component = $this->get("components.Contratos");
        $entities = $component->getAll();

        return $this->render('RebeccaBundle:Contratos:index.html.twig', array(
                    'entities' => $entities
                ));
    }

    /**
     * Finds and displays a Contratos entity.
     *
     */
    public function showAction($id) {
        /* @var $component Rebecca\RebeccaBundle\Component\ContratosComponent */
        $component = $this->get("components.Contratos");

        $entity = $component->get($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Contratos entity.');
        }

        $deleteForm = $this->createDeleteForm($id);

        return $this->render('RebeccaBundle:Contratos:show.html.twig', array(
                    'entity' => $entity,
                    'delete_form' => $deleteForm->createView(),
                ));
    }

    /**
     * Displays a form to create a new Contratos entity.
     *
     */
    public function newAction($id) {
        $entity = new Contratos();
        /* @var $component Rebecca\RebeccaBundle\Component\TrabajadoresComponent */
        $component = $this->get("components.Trabajadores");

        $entity->setIdTrabajador($component->get($id));

        $form = $this->createForm(new ContratosType(), $entity);

        return $this->render('RebeccaBundle:Contratos:new.html.twig', array(
                    'entity' => $entity,
                    'form' => $form->createView()
                ));
    }

    /**
     * Creates a new Contratos entity.
     *
     */
    public function createAction() {
        $entity = new Contratos();
        $request = $this->getRequest();

        /* @var $component Rebecca\RebeccaBundle\Component\TrabajadoresComponent */
        $component = $this->get("components.Trabajadores");

        $entity->setIdTrabajador($component->get($request->get('id')));

        $form = $this->createForm(new ContratosType(), $entity);
        $form->bindRequest($request);

        if ($form->isValid()) {
            /* @var $component Rebecca\RebeccaBundle\Component\ContratosComponent */
            $component = $this->get("components.Contratos");
            $component->save($entity);
            $component->commit();

            return $this->redirect($this->generateUrl('contratos'));
        }

        return $this->render('RebeccaBundle:Contratos:new.html.twig', array(
                    'entity' => $entity,
                    'form' => $form->createView()
                ));
    }

    /**
     * Displays a form to edit an existing Contratos entity.
     *
     */
    public function editAction($id) {
        /* @var $component Rebecca\RebeccaBundle\Component\ContratosComponent */
        $component = $this->get("components.Contratos");
        $entity = $component->get($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Contratos entity.');
        }

        $editForm = $this->createForm(new ContratosType(), $entity);
        $deleteForm = $this->createDeleteForm($id);

        return $this->render('RebeccaBundle:Contratos:edit.html.twig', array(
                    'entity' => $entity,
                    'edit_form' => $editForm->createView(),
                    'delete_form' => $deleteForm->createView(),
                ));
    }

    /**
     * Edits an existing Contratos entity.
     *
     */
    public function updateAction($id) {
        /* @var $component Rebecca\RebeccaBundle\Component\ContratosComponent */
        $component = $this->get("components.Contratos");

        $entity = $component->get($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Contratos entity.');
        }

        $editForm = $this->createForm(new ContratosType(), $entity);
        $deleteForm = $this->createDeleteForm($id);

        $request = $this->getRequest();

        $editForm->bindRequest($request);

        if ($editForm->isValid()) {
            $component->update($entity);
            $component->commit();

            return $this->redirect($this->generateUrl('contratos'));
        }

        return $this->render('RebeccaBundle:Contratos:edit.html.twig', array(
                    'entity' => $entity,
                    'edit_form' => $editForm->createView(),
                    'delete_form' => $deleteForm->createView(),
                ));
    }

    /**
     * Deletes a Contratos entity.
     *
     */
    public function deleteAction($id) {
        $form = $this->createDeleteForm($id);
        $request = $this->getRequest();

        $form->bindRequest($request);

        if ($form->isValid()) {
            /* @var $component Rebecca\RebeccaBundle\Component\ContratosComponent */
            $component = $this->get("components.Contratos");
            $entity = $component->get($id);

            if (!$entity) {
                throw $this->createNotFoundException('Unable to find Contratos entity.');
            }

            $component->delete($entity);
            $component->commit();
        }

        return $this->redirect($this->generateUrl('contratos'));
    }

    private function createDeleteForm($id) {
        return $this->createFormBuilder(array('id' => $id))
                        ->add('id', 'hidden')
                        ->getForm()
        ;
    }

    public function realizarPagoAction($id) {
        /* @var $component Rebecca\RebeccaBundle\Component\ContratosComponent */
        $component = $this->get("components.Contratos");

        /* @var $componentTipoNomina Rebecca\RebeccaBundle\Component\TiposNominaComponent */
        $componentTipoNomina = $this->get("components.TiposNomina");

        $entity = $component->get($id);

        $tipoNomina = $componentTipoNomina->getFull($entity->getIdTipoNomina()->getId());

        $entity->setIdTipoNomina($tipoNomina);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Contratos entity.');
        }

        $deleteForm = $this->createDeleteForm($id);

        return $this->render('RebeccaBundle:Contratos:realizarpago.html.twig', array(
                    'entity' => $entity,
                    'delete_form' => $deleteForm->createView(),
                ));
    }

    public function ParametrosAction($id) {
        /* @var $component Rebecca\RebeccaBundle\Component\ContratosComponent */
        $component = $this->get("components.Contratos");

        $idPeriodo = $this->getRequest()->get("idPeriodo");
        
        $entity = $component->get($id);
        $periodos = $component->getAllPeriodos($id);
        $parametrosRegistrados = array();
        
        $hayPeriodo = false;
        
        $periodoActual = null;
        $parametros = null;

        if (isset($idPeriodo)){
            $hayPeriodo = true;
            
            $periodoActual = $component->getPeriodo($idPeriodo);;
            $parametros = $component->getParametros($id, $idPeriodo);
            
            $parametrosRegistrados = $component->getParametrosRegistrados($id, $idPeriodo);
            
        }

        return $this->render('RebeccaBundle:Contratos:parametros.html.twig', array(
                    'entity' => $entity,
                    'parametros' => $parametros,
                    'hayPeriodo' => $hayPeriodo,
                    'periodos' => $periodos,
                    'periodoActual' => $periodoActual,
                    'parametrosRegistrados' => $parametrosRegistrados
                ));
    }

    public function addParametrosAction() {
        /* @var $component Rebecca\RebeccaBundle\Component\ContratosComponent */
        $component = $this->get("components.Contratos");

        $idParametro = $this->getRequest()->get("idParametro");
        $idPeriodo = $this->getRequest()->get("idPeriodo");
        $idContrato = $this->getRequest()->get("idContrato");
        $formula = $this->getRequest()->get("formula");

        $component->addParametrosRegistrados($idParametro, $idPeriodo, $idContrato, $formula);
        
        return $this->redirect($this->generateUrl('contratos_parametros', array('id' => $idContrato, 'idPeriodo' => $idPeriodo)));
    }

    public function editParametrosAction() {
        /* @var $component Rebecca\RebeccaBundle\Component\ContratosComponent */
        $component = $this->get("components.Contratos");
        
        $id = $this->getRequest()->get("idParametroRegistrado");
        $formula = $this->getRequest()->get("formula");

        $component->addParametrosRegistrados($id, $formula);
    }

}
